# -*- coding;utf-8 -*-
"""
File name : AddQuestionScaleForImage.py.PY
Program IDE : PyCharm
Create file time: 2024/3/24 23:54
File Create By Author : 朱清闯
"""
from question.models import Questions
from PIL import Image
import os
from django.conf import settings


def picture2scale(where):
    try:
        with Image.open(where) as image:
            width = image.size[0]
            height = image.size[1]
            return str(width) + '@' + str(height)
    except FileNotFoundError:
        print(f"图片不存在，跳过处理: {where}")
        # 图片不存在，则返回error，删除该图片信息
        return 'error'


def add_scale(question_server_list):
    # question_server_list = Questions.objects.all()
    for question in question_server_list:
        pictures = question.pictures
        pictures_answer = question.pictures_answer
        pictures_explain = question.pictures_explain
        # 有需要保存到标志
        save_flag = False
        scale_str = ''
        if len(pictures) > 0 and '-' not in pictures:
            where = os.path.join(settings.BASE_DIR).replace('\\', '/') + pictures
            result = picture2scale(where=where)
            if result != 'error':
                question.pictures_scale = result
                save_flag = True
            else:
                question.pictures = ''
                question.pictures_scale = ''
                save_flag = True
        elif len(pictures) > 0 and '-' in pictures:
            pictures = pictures.split("-")
            for where in pictures:
                where = os.path.join(settings.BASE_DIR).replace('\\', '/') + where
                result = picture2scale(where=where)
                if result != 'error':
                    scale_str += result + '-'
                    save_flag = True
                else:
                    question.pictures = ''
                    question.pictures_scale = ''
                    save_flag = True
                    scale_str = ''
                    break
            if len(scale_str) > 0:
                scale_str = scale_str[:len(scale_str) - 1]
                question.pictures_scale = scale_str
        scale_answer_str = ''
        if len(pictures_answer) > 0 and '-' not in pictures_answer:
            where = os.path.join(settings.BASE_DIR).replace('\\', '/') + pictures_answer
            result = picture2scale(where=where)
            if result != 'error':
                question.pictures_answer_scale = result
                save_flag = True
            else:
                question.pictures_answer = ''
                question.pictures_answer_scale = ''
                save_flag = True
        elif len(pictures_answer) > 0 and '-' in pictures_answer:
            pictures_answer = pictures_answer.split("-")
            for where in pictures_answer:
                where = os.path.join(settings.BASE_DIR).replace('\\', '/') + where
                result = picture2scale(where=where)
                if result != 'error':
                    scale_answer_str += result + '-'
                    save_flag = True
                else:
                    question.pictures_answer = ''
                    question.pictures_answer_scale = ''
                    save_flag = True
                    scale_answer_str = ''
                    break
            if len(scale_str) > 0:
                scale_answer_str = scale_answer_str[:len(scale_answer_str) - 1]
                question.pictures_answer_scale = scale_answer_str
        scale_explain_str = ''
        if len(pictures_explain) > 0 and '-' not in pictures_explain:
            where = os.path.join(settings.BASE_DIR).replace('\\', '/') + pictures_explain
            result = picture2scale(where=where)
            if result != 'error':
                question.pictures_explain_scale = result
                save_flag = True
            else:
                question.pictures_explain = ''
                question.pictures_explain_scale = ''
                save_flag = True
        elif len(pictures_explain) > 0 and '-' in pictures_explain:
            pictures_explain = pictures_explain.split("-")
            for where in pictures_explain:
                where = os.path.join(settings.BASE_DIR).replace('\\', '/') + where
                result = picture2scale(where=where)
                if result != 'error':
                    scale_explain_str += result + '-'
                    save_flag = True
                else:
                    question.pictures_explain = ''
                    question.pictures_explain_scale = ''
                    save_flag = True
                    scale_explain_str = ''
                    break
            if len(scale_explain_str) > 0:
                scale_answer_str = scale_answer_str[:len(scale_answer_str) - 1]
                question.pictures_explain = scale_answer_str
        if save_flag:
            question.save()
    return question_server_list


if __name__ == "__main__":
    add_scale()
